package com.example.springboot.moduleone.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.config.RedisConfig;
import com.example.springboot.config.Result;
import com.example.springboot.config.WebConstant;
import com.example.springboot.moduleone.pojo.module;
import com.example.springboot.moduleone.service.moduleService;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/modou")
public class moduleController {
    private final moduleService moduleService;
    private final StringRedisTemplate stringRedisTemplate;

    public moduleController(moduleService moduleService, StringRedisTemplate stringRedisTemplate) {
        this.moduleService = moduleService;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 查询所有模块
     * @return
     */
    @GetMapping(value = "/module")
    public Result<?> queryModule() {
        return Result.OK(moduleService.list());
    }

    /**
     * 分页查询模块
     * @param pageNum
     * @param pageSize
     * @param module
     * @return
     */
    @GetMapping(value = "/modulePage")
    public Result<?> queryModulePage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestBody module module ) {
        QueryWrapper<module> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("name",module.getName());
        Page<module> page = new Page<>(pageNum,pageSize);
        return Result.OK(moduleService.page(page,objectQueryWrapper));
    }

    /**
     * 新增模块
     */
    @PostMapping(value = "/addition")
    @Transactional
    public Result<?>  addModule(@RequestBody module module) {
        moduleService.save(module);
        return Result.OK();
    }

    /**
     * 删除模块
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> deleteModule(@PathVariable String id) {
        return Result.OK(moduleService.removeById(id));
    }

    /**
     * 批量删除模块
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestBody String[] ids) {
        return Result.OK(moduleService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 更新模块
     * @param module
     * @return
     */
    @PutMapping(value = "/update")
    public Result<?> updateModule(@RequestBody module module) {
        return Result.OK(moduleService.updateById(module));
    }

    /**
     * 根据id查询模块
     * @param id
     * @return
     */
    @GetMapping(value = "/module_By_id/{id}")
    public Result<?> queryModuleById(@PathVariable String id) {
        return Result.OK(moduleService.getById(id));
    }

    /**
     * 测试Redis缓存
     */
    @GetMapping(value = "/testRedis")
    @ResponseBody
        public Result<?> testRedis(String name) {
        // 单项缓存
        stringRedisTemplate.opsForValue().set("这是个包:王五1","123456");
        //存json 方式
        stringRedisTemplate.opsForValue().set("这是个包:王五2","{\"name\":\"张三\",\"age\":18}");
        //存入List
        stringRedisTemplate.opsForList().leftPush("这是个包:王五3","1");
        //设置过期时间
        stringRedisTemplate.opsForValue().set("这是个包:王五4","{\"name\":\"张三\",\"age\":18}",3, TimeUnit.SECONDS);

        return Result.OK(stringRedisTemplate.opsForValue().get(name));
    }

}
